package com.sanyang.pda.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.sanyang.pda.bean.ScanBean;
import com.sanyang.pda.view.ScanButView;
import com.wangshen.base.dialog.base.BaseDialog;
import com.wangshen.base.util.ToastUtils;
import com.wangshen.projectbase.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ScatteredDialog extends BaseDialog {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_close)
    ImageView ivClose;

    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_tray)
    TextView tvTray;
    @BindView(R.id.tv_small_location)
    TextView tvSmallLocation;
    @BindView(R.id.tv_scattered_location)
    TextView tvScatteredLocation;
    @BindView(R.id.sv_zheng_scan)
    ScanButView svZhengScan;
    @BindView(R.id.sv_chai_scan)
    ScanButView svChaiScan;
    private OnScatteredClick onScatteredClick;
    private int type=0;
    public ScatteredDialog(@NonNull Context context, OnScatteredClick onScatteredClick) {
        super(context);
        this.onScatteredClick = onScatteredClick;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        svZhengScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                switch (scanBean.getType()) {
                    case 1://存储库位
                        if (type == 0 || type == 1)
                        tvSmallLocation.setText(scanBean.getCode());
                        break;
                    case 4://托盘
                        tvTray.setText(scanBean.getCode());
                        break;

                }
            }
        });
        svChaiScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                switch (scanBean.getType()) {
                    case 1://存储库位
                        if (type == 2)
                        tvScatteredLocation.setText(scanBean.getCode());
                        break;
                    case 4://托盘
                        tvTray.setText(scanBean.getCode());
                        break;

                }
            }
        });
        svChaiScan.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                type=2;
                return false;
            }
        });
        svZhengScan.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                type=1;
                return false;
            }
        });

    }

    @Override
    public void initData() {

    }

    @Override
    public int getView() {
        return R.layout.dialog_scattered;
    }

    @OnClick(R.id.iv_close)
    public void onIvCloseClicked() {
        dismiss();
    }

    @OnClick(R.id.tv_cancel)
    public void onTvCancelClicked() {
        dismiss();
    }

    @OnClick(R.id.tv_confirm)
    public void onTvConfirmClicked() {
        if (TextUtils.isEmpty(tvTray.getText().toString())) {
            ToastUtils.showToast(getContext(), getContext().getResources().getString(R.string.tray) + getContext().getResources().getString(R.string.not_be_empty));
            return;
        }
        if (TextUtils.isEmpty(tvSmallLocation.getText().toString())) {
            ToastUtils.showToast(getContext(), getContext().getResources().getString(R.string.small_location) + getContext().getResources().getString(R.string.not_be_empty));
            return;
        }
        if (TextUtils.isEmpty(tvScatteredLocation.getText().toString())) {
            ToastUtils.showToast(getContext(), getContext().getResources().getString(R.string.scattered_location) + getContext().getResources().getString(R.string.not_be_empty));
            return;
        }
        if (onScatteredClick != null) {
            onScatteredClick.onConfirm(tvTray.getText().toString(), tvSmallLocation.getText().toString(), tvScatteredLocation.getText().toString());
            dismiss();
        }
    }

    public interface OnScatteredClick {
        void onConfirm(String tray, String smalllocation, String scatteredlocation);
    }
}
